// pages/home/shelf/shelf.js
var util = require('../../../utils/util.js')
var app = getApp();
var shelfQrcode = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectAllStatus: false, //全选状态
    subjects: [],
    itemNumbers: [],
    objData: {
      itemNumber: '',
      pictureUrl: '',
      productDesc: '',
      productName: '',
      qty: 1,
      status: 0,
      sysNo: '',
      vendorCode: '',
      vendorName: '',
      is_select: false
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   * 
   * 
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},

  /*点击扫描--展示页面 */
  addQrcode() {
    var that = this;
    wx.scanCode({
      success(res) {
        util.getRequest('shelf/productInfo/' + res.result + '/true', '', 'GET', function(result) {
          if (result.success) {
            console.log(result)
            var objData = result.data;
            var _is = false;
            objData.qty = 1;
            let subjects = that.data.subjects;
            if (subjects != null && subjects.length > 0) {
              for (var i = subjects.length - 1; i >= 0; i--) {
                if (objData.itemNumber == subjects[i].itemNumber) {
                  _is = true;
                  break;
                }
              }
              if (_is == true) {
                //subjects.splice(i, 1);
                wx.showToast({
                  title: '该商品已存在',
                  icon: 'none',
                });
                return;
              } else {
                subjects.unshift(objData);
                that.setData({
                  subjects
                });
                return;
              }
            } else {
              subjects.unshift(objData);
              that.setData({
                subjects
              });
            }
          } else {
            wx.showToast({
              title: result.message,
              icon: 'none',
            })
          }
        })
      }
    })
  },

  /**
   * 绑定加数量事件
   */
  btn_add(e) {
    // 获取点击的索引
    const index = e.currentTarget.dataset.index;
    // 获取商品数据
    let subjects = this.data.subjects;
    // 获取商品数量
    let qty = subjects[index].qty;
    // 点击递增
    qty = qty + 1;
    subjects[index].qty = qty;
    // 重新渲染 ---显示新的数量
    this.setData({
      subjects: subjects
    });
  },

  /**
   * 绑定减数量事件
   */
  btn_minus(e) {
    //获取点击的索引
    const index = e.currentTarget.dataset.index;
    // 获取商品数据
    let subjects = this.data.subjects;
    let qty = subjects[index].qty;
    // 判断num小于等于1  return; 点击无效
    if (qty <= 1) {
      return false;
    }
    // else  num大于1  点击减按钮  数量--
    qty = qty - 1;
    subjects[index].qty = qty;
    // 渲染页面
    this.setData({
      subjects: subjects
    });
  },

  /**
   * 当前商品选中事件
   */
  selectIcon(e) {
    var that = this;
    // 获取选中的radio索引
    var index = e.currentTarget.dataset.index;
    // 获取到商品列表数据
    var subjects = that.data.subjects;
    // 默认全选
    that.data.selectAllStatus = true;
    // 循环数组数据，判断----选中/未选中[selected]
    subjects[index].is_select = !subjects[index].is_select;
    // 如果数组数据全部为selected[true],全选
    for (var i = subjects.length - 1; i >= 0; i--) {
      if (!subjects[i].is_select) {
        that.data.selectAllStatus = false;
        break;
      }
    }
    // 重新渲染数据
    that.setData({
      subjects: subjects,
      selectAllStatus: that.data.selectAllStatus
    })
  },
  /**
   * 上架操作
   */
  btn_out_selected() {
    var that = this;
    var subjects = that.data.subjects;
    var Product;
    if (subjects != null && subjects.length > 0) {
      for (var subj in subjects) {
        if (subjects[subj].is_select) {
          Product = {
            itemNumber: subjects[subj].itemNumber,
            qty: subjects[subj].qty,
          }
          that.data.itemNumbers.push(Product);
        }
      }
      if (that.data.itemNumbers.length > 0) {
        for (var itNum in that.data.itemNumbers) {
          if (that.data.itemNumbers[itNum].qty == 0) {
            wx.showToast({
              title: "请输入正确数量",
              icon: 'none',
            })
            return
          }
        }
        this.onShelf();
      } else {
        wx.showToast({
          title: "请选择要上架的商品",
          icon: 'none',
        })
      }
    } else {
      wx.showToast({
        title: "当前无商品，请添加商品",
        icon: 'none',
      })
    }
  },
  /**
   全部上架
   */
  btn_out_all() {
    var that = this;
    var subjects = that.data.subjects;
    var Product;
    if (subjects != null && subjects.length > 0) {
      for (var subj in subjects) {
        Product = {
          itemNumber: subjects[subj].itemNumber,
          qty: subjects[subj].qty
        }
        that.data.itemNumbers.push(Product);
      }
      for (var itNum in that.data.itemNumbers) {
        if (that.data.itemNumbers[itNum].qty==0){
          wx.showToast({
            title: "请输入正确数量",
            icon: 'none',
          })
          return
        }
      }
      this.onAllShelf();
    } else {
      wx.showToast({
        title: "当前无商品，请添加商品",
        icon: 'none',
      })
    }
  },
  /**
   * 上架成功
   */
  onShelf() {
    var that = this;
    var itemNumbers = that.data.itemNumbers;
    var subjects = that.data.subjects;
    var paramsBady = {
      "shelfCode": app.globalData.shelfQrcode,
      "type": '0',
      "itemNumbers": itemNumbers
    }
    util.getRequest('shelf/itemShelf', paramsBady, 'POST', function(result) {
      if (result.success) {
        if (subjects != null && subjects.length > 0) {
          for (var ite = subjects.length - 1; ite >= 0; ite--) {
            if (subjects[ite].is_select) {
              subjects.splice(ite, 1);
            }
          }
          that.setData({
            subjects: subjects,
            itemNumbers: []
          })
        }
        wx.showToast({
          title: "上架成功",
          icon: 'success',
        })

      } else {
        wx.showToast({
          title: result.message,
          icon: 'none',
        })
      }
    })
  },

  /**
   *全部上架成功
   */
  onAllShelf() {
    var that = this;
    var subjects = that.data.subjects;
    var itemNumbers = that.data.itemNumbers;
    var paramsBady = {
      "shelfCode": app.globalData.shelfQrcode,
      "type": '0',
      "itemNumbers": itemNumbers
    }
    util.getRequest('shelf/itemShelf', paramsBady, 'POST', function(result) {
      if (result.success) {
        that.setData({
          subjects: [],
          itemNumbers: []
        });
        wx.showToast({
          title: "一键上架成功",
          icon: 'success',
        })
      } else {
        wx.showToast({
          title: result.message,
          icon: 'none',
        })
      }
    })
  },

  bindKeyInput: function (e) {
    const index = e.currentTarget.dataset.index;
    let subjects = this.data.subjects;
    let qty = subjects[index].qty;
    qty = e.detail.value;
    if(qty==0){
      wx.showToast({
        title: "请输入正确数量",
        icon: 'none',
      })
    }
    subjects[index].qty = qty;
    this.setData({
      subjects: subjects
    });
  }
})